//
//  XHRegisteredUserViewController.m
//  xidian
//
//  Created by 冷山 on 16/5/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHRegisteredUserViewController.h"

#import "XHRegisteredButtonCell.h"
#import "XHRegisteredCell.h"

#import "Services.h"

#define kNameTextFieldTag 1000
#define kCompanyTextFieldTag    1002
#define kDepartmentTextFieldTag 1003
#define kInviteCodeTextFieldTag 1004


@interface XHRegisteredUserViewController ()<XHRegisteredButtonCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;

@property (nonatomic, strong) NSString * m_phone;
@property (nonatomic, strong) NSString * m_name;
@property (nonatomic, strong) NSString * m_company;
@property (nonatomic, strong) NSString * m_department;
@property (nonatomic, strong) NSString * m_inviteCode;

@end

@implementation XHRegisteredUserViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHRegisteredCell" bundle:nil] forCellReuseIdentifier:XHRegisteredCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHRegisteredButtonCell" bundle:nil] forCellReuseIdentifier:XHRegisteredButtonCellIdentifier];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
    
    if (section == 0)
    {
        rows = 5;
    }
    else if (section == 1)
    {
        rows = 1;
    }
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (indexPath.section == 0)
    {
        cell=[self registeredCell:indexPath];
    }
    else
    {
        cell=[self registeredButtonCell];
    }
    return cell;
}

- (XHRegisteredCell *)registeredCell:(NSIndexPath*)indexPath
{
    XHRegisteredCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHRegisteredCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0)
    {
        cell.ui_titleLabel.text=@"电话";
        cell.ui_textFields.text = [DataManager sharedInstance].userId;
    }
    else if (indexPath.row == 1)
    {
        cell.ui_titleLabel.text=@"姓名";
        cell.ui_textFields.tag = kNameTextFieldTag;
        cell.ui_textFields.text = self.m_name;
    }
    else if (indexPath.row == 2)
    {
        cell.ui_titleLabel.text=@"公司";
        cell.ui_textFields.tag = kCompanyTextFieldTag;
        cell.ui_textFields.text = self.m_company;
    }
    else if (indexPath.row == 3)
    {
        cell.ui_titleLabel.text=@"所属部门";
        cell.ui_textFields.tag = kDepartmentTextFieldTag;
        cell.ui_textFields.text = self.m_department;
    }
    else if (indexPath.row == 4)
    {
        cell.ui_titleLabel.text=@"邀请码";
        cell.ui_textFields.tag = kInviteCodeTextFieldTag;
        cell.ui_textFields.keyboardType = UIKeyboardTypeASCIICapable;
        cell.ui_selectImage.hidden = NO;
        cell.ui_textFields.text = self.m_inviteCode;
    }
    return cell;
}

- (XHRegisteredButtonCell*)registeredButtonCell
{
    XHRegisteredButtonCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHRegisteredButtonCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=50;
    if(indexPath.section == 1)
    {
        height = 80;
    }
    return height;
}

- (void)textFieldTextDidChangeNotification:(NSNotification *)notif
{
    UITextField * textField = (UITextField *)[notif object];
    switch (textField.tag)
    {
        case kNameTextFieldTag:
            self.m_name = textField.text;
            break;
            
        case kCompanyTextFieldTag:
           self.m_company = textField.text;
            break;
            
        case kDepartmentTextFieldTag:
            self.m_department = textField.text;
            break;
            
        case kInviteCodeTextFieldTag:
           self.m_inviteCode = textField.text;
            break;
    }
}

#pragma - mark XHRegisteredButtonCellDelegate

//提交注册信息
- (void)reguatered:(UIButton *)button
{
    if (![NSString checkNullString:self.m_name]&&
        ![NSString checkNullString:self.m_company]&&
        ![NSString checkNullString:self.m_department])
    {
        if ([NSString checkNullString:self.m_inviteCode])
        {
            self.m_inviteCode = @"";
        }
        [ServiceManager postRegistrationInformationWithName:self.m_name phone:[DataManager sharedInstance].userId company:self.m_company department:self.m_department inviteCode:self.m_inviteCode success:^(NSDictionary *data) {
            [PromptAction showSuccessPrompt:@"上传成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSDictionary *data) {
            [PromptAction showInfoPrompt:@"上传失败!"];
        }];
    }
    else if ([NSString checkNullString:self.m_name])
    {
        [PromptAction showInfoPrompt:@"姓名不能为空"];
    }
    else if ([NSString checkNullString:self.m_company])
    {
        [PromptAction showInfoPrompt:@"公司不能为空"];
    }
    else if ([NSString checkNullString:self.m_department])
    {
        [PromptAction showInfoPrompt:@"部门不能为空"];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
